import {Injectable} from '@angular/core';
import * as sha1 from 'js-sha1';
import {Router, ActivatedRoute} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {EventsService} from "@core/events/events.service";

const HAS_LOGGED_IN = 'hasLoggedIn';
@Injectable()
export class AuthService {
  public timestamp: Date;
  private _redirectUrl: string;
  // isLoggedIn: string;
  // public HAS_LOGGED_IN = 'hasLoggedIn';



  constructor(
    public tools: ToolsService,
    public router: Router,
    public route: ActivatedRoute,
    public events: EventsService
  ) {
  }
  urlHandler(response, url?) {
    if (!response) {
      return;
    }
    let res = response.json();
    let currentUrl = this.router.url || null;
    (currentUrl !== '/auth/login') && currentUrl !== '/'  &&  (this.redirectUrl = currentUrl);

    // if (res && res.result) {
    //   if (
    //     res.result === '2101'
    //     || res.result === '2103'
    //     || res.result === '2105'
    //     || res.result === '4000'
    //   ) {
    //     this.logout();
    //     this.tools.presentConfirm('此操作需要登录,是否前往登录', 1, this.goToLogin.bind(this))
    //   }
    // }
    if (res) {
      switch (res.result) {
        case '2101':
        case '2103':
        case '2105':
        case '4000':
          this.logout();
          this.tools.presentConfirm('此操作需要登录,是否前往登录', 1, this.goToLogin.bind(this));
      }
    }
  }



  selectGoToLogin() {
    this.tools.presentConfirm('此操作需要登录,是否前往登录', 1, this.goToLogin.bind(this));
  }


  goToLogin() {
    this.logout();
    this.router.navigate(['/auth/login']);
  }



  setUserId(userId: string) {
    localStorage.setItem('userId', userId);
  }

  getUserId() {
    return localStorage.getItem('userId');
  }

  setToken(token: string) {
    localStorage.setItem('token', token);
  }

  setOpenId(openId: string) {
    localStorage.setItem('openId', openId);
  }

  getOpenId(): string {
    return localStorage.getItem('openId');
  }

  getToken() {
    return localStorage.getItem('token');
  }

  setRefreshToken(refreshToken: string) {
    localStorage.setItem('refreshToken', refreshToken);
  }

  getRefreshToken() {
    return localStorage.getItem('refreshToken');
  }

  login(userid: string, token: string, refreshToken: string = '', openId: string = '') {
    this.setUserId(userid);
    this.setToken(token);
    this.setRefreshToken(refreshToken);
    this.setLogin('true');
    if (openId) {
      this.setOpenId(openId);
    };
  }


  logout() {
    localStorage.clear();
  }

  get redirectUrl(): string {
    return this._redirectUrl;
  }

  set redirectUrl(value: string) {
    this._redirectUrl = value;
  }


  setLogin(key: string) {
    localStorage.setItem(HAS_LOGGED_IN, key);
    // this.isLoggedIn = key;
  }

  get isLoggedIn(): boolean  {
    if (this.hasLoggedIn() === 'true') {
      return true;
    } else {
      return false;
    }
  }

 /* set isLoggedIn(value: boolean) {
    this._isLoggedIn = value;
  }*/


  hasLoggedIn() {
    return localStorage.getItem(HAS_LOGGED_IN);
  }

  buildSignature(userid: string, token: string, timestamp) {
    return sha1(userid + token + timestamp);
  }

  setAuthOrgId(authOrgId: string) {
    localStorage.setItem('auth_orgId', authOrgId);
  }

  getAuthOrgId() {
    return localStorage.getItem('auth_orgId');
  }


}
